<?php


namespace app\common\logic;


class SendMessage extends Wework
{
    protected $msgtype = null;
    protected $toall = 0;
    protected $content = null;
    protected $access_token = null;
    protected $agentid = null;
    protected $to_student_userid = null;


    protected $data = [];

    public function __construct($access_token,$agentid)
    {
        $this->agentid = $agentid;
        $this->assess_token = $access_token;
    }

    public function userid($to_student_userid)
    {
        $this->to_student_userid = $to_student_userid;
    }

    /**
     * 消息类型
     * text image voice video file  news  mpnews  miniprogram
     */
    public function text($content)
    {
        $this->msgtype = 'text';
        $this->data['text'] = ['content'=>$content];
    }


    public function image($media_id)
    {
        $this->msgtype = 'image';
        $this->data['image'] = ['media_id'=>$media_id];
    }

    public function voice($media_id)
    {
        $this->msgtype = 'voice';
        $this->data['voice'] = ['media_id'=>$media_id];
    }


    public function video($media_id,$title = '',$description = '')
    {
        $this->msgtype = 'video';
        $this->data['video'] = ['media_id'=>$media_id,'title'=>$title,'description'=>$description];
    }

    public function file($media_id)
    {
        $this->msgtype = 'file';
        $this->data['file'] = ['media_id'=>$media_id];
    }


    public function news($title,$description = '',$picurl = '',$url = '')
    {
        $this->msgtype = 'news';
        $this->data['news'] = ['articles'=>array(['title'=>$title,'description'=>$description,'url'=>$url,'picurl'=>$picurl])];
    }


    public function mpnews($title,$thumb_media_id,$content,$author = '',$content_source_url = '',$digest = '')
    {
        $this->msgtype = 'mpnews';
        $this->data['mpnews'] = ['articles'=>array(['title'=>$title,'thumb_media_id'=>$thumb_media_id,'content'=>$content,'author'=>$author,'content_source_url'=>$content_source_url,'digest'=>$digest])];
    }



    public function toall()
    {
        $this->toall = 1;
    }




    public function send()
    {
        self::init();
        $param = array_merge([
            'toall'             =>$this->toall,
            'agentid'           =>$this->agentid,
            'msgtype'           =>$this->msgtype,
            'to_student_userid' =>$this->to_student_userid,
        ],$this->data);

        $_res = \fast\Http::post(self::$baseUrl."cgi-bin/externalcontact/message/send?access_token={$this->assess_token}",json_encode($param));
        $res = json_decode($_res,true);
        return $res;


    }




}